<template>
  <a-modal
    :title="title"
    :visible="visible"
    @cancel="handleCancel"
    :width="width"
    :confirmLoading="confirmLoading"
    v-drag
  >
    <template slot="footer">
      <a-button @click="handleCancel">{{$t('关闭')}}</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="getList">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('商品分类')">
              <a-tree-select
                allowClear
                v-model="queryParam.parentKindId"
                :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                :tree-data="treeData"
                :placeholder="$t('请输入商品分类')"
              />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('商品名称')">
              <a-input :placeholder="$t('请输入商品编码/名称/条码')" v-model="queryParam.goodsInfo"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-button type="primary" @click="getList" icon="search">{{$t('查询')}}</a-button>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-button type="primary" style="float:right" @click="add">{{$t('添加')}}</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="false"
        :loading="false"
      >
        <span slot="action" slot-scope="text, record">
          <a-popconfirm :title="$t('确定删除吗?')" @confirm="() => handleDelete(record.id)">
            <a>X</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    </a-spin>
    <add-modal ref="addModal" @ok="getList"></add-modal>
  </a-modal>
</template>

<script>
import { httpAction, postAction, getAction } from '@/api/manage'
import { filterMultiDictText, initDictOptions } from '@/components/dict/JDictSelectUtil'
import pick from 'lodash.pick'
import AddModal from './AddModal'
import { deleteAction } from '../../../api/manage'
export default {
  name: 'ExchangeGoodsModal',
  components: {
    AddModal
  },
  data() {
    return {
      title: this.$t('添加'),
      confirmLoading: false,
      width: 1200,
      treeData: [],
      visible: false,
      queryParam:{},
      columns: [
        {
          title: this.$t('商品编码'),
          align: 'center',
          dataIndex: 'bisGoodsInfor.code',
        },{
          title: this.$t('商品条码'),
          align: 'center',
          dataIndex: 'bisGoodsInfor.barCode',
        },{
          title: this.$t('商品名称'),
          align: 'center',
          dataIndex: 'bisGoodsInfor.name',
        },{
          title: this.$t('商品分类'),
          align: 'center',
          dataIndex: 'bisGoodsInfor.kindName',
        },{
          title: this.$t('积分值'),
          align: 'center',
          dataIndex: 'bisGoodsInfor.integral',
        },
        {
          title: this.$t('操作'),
          dataIndex: 'action',
          align: 'center',
          width: '100',
          scopedSlots: { customRender: 'action' },
        },
      ],
      dataSource: [],
    }
  },
  created() {
  },
  methods: {
    show(record) {
      this.model=Object.assign({},record)
      this.queryParam={
        zone:this.model.value
      }
      this.visible = true
      this.select()
      this.getList()
    },
    select() {
      let that = this
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.column = 'sort'
      param.order = 'asc'
      getAction('/goods/bisGoodsKind/rootList', param)
        .then((res) => {
          if (res.success) {
            that.allChildKind = []
            if (res.result.records.length > 0) {
              res.result.records.forEach((col) => {
                col.key = '' + col.id
                col.value = '' + col.id
                col.title = '' + col.name
                col.id = '' + col.id
                col.pid = '' + col.pid
                col.hasChild = '' + col.hasChild
                if (col.hasChild != '0') {
                  that.getHasChild(col)
                }
              })
              that.treeData = that.getDataByResult(res.result.records)
            } else {
              that.treeData = []
            }
          } else {
            that.$alert(res.message, '提示', {
              confirmButtonText: '确定',
              callback: (action) => {},
            })
          }
        })
        .catch((error) => {
          console.log(error)
        })
    },
    getHasChild(record) {
      let that = this
      let query = {}
      query.pageNo = 1
      query.pageSize = 9999
      query.pid = record.id
      getAction('/goods/bisGoodsKind/rootList', query)
        .then((res) => {
          if (res.success) {
            if (res.result.records.length > 0) {
              res.result.records.forEach((col) => {
                col.key = '' + col.id
                col.value = '' + col.id
                col.title = '' + col.name
                col.pid = '' + col.pid
                col.id = '' + col.id
                col.hasChild = '' + col.hasChild
                if (col.hasChild != '0') {
                  that.getHasChild(col)
                }
              })
              record.children = that.getDataByResult(res.result.records)
              that.treeData = [...that.treeData]
            }
          } else {
            that.$message.warning(res.message)
          }
        })
        .catch((error) => {
          console.log(error)
        })
    },
    getDataByResult(result) {
      let that = this
      return result.map((item) => {
        if (item.hasChild == 0 && item.key != '0') {
          that.allChildKind.push(item.key)
        }
        return item
      })
    },
    getList() {
      let that = this;
      that.queryParam.pageSize=99999999
      getAction('/goods/bisGoodsZone/list', that.queryParam).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
        }
      })
    },
    handleDelete(id){
      deleteAction('/goods/bisGoodsZone/delete',{id:id}).then(res=>{
        if(res.success){
          this.$message.success(res.message)
          this.getList()
        }else{          
          this.$message.error(res.message)
        }
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    add(){
      this.$refs.addModal.show({zone:this.model.value})
    }
  },
}
</script>
<style scoped>
</style>